package com.baitiaojun.bulletscreen.config;

import com.baitiaojun.bulletscreen.config.properties.MqBulletScreenProperties;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @类名 RabbitmqConfig
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/11/26 20:05
 * @版本 1.0
 */
@Configuration
public class RabbitmqBulletScreenConfig {

    @Resource
    private MqBulletScreenProperties properties;

    @Bean
    public Exchange bulletScreenExchange () {
        return ExchangeBuilder.topicExchange(properties.getExchange())
                .durable(true)
                .alternate(properties.getBackupExchange())
                .build();
    }

    @Bean
    public Queue bulletScreenQueue() {
        return QueueBuilder.durable(properties.getQueue())
                .deadLetterExchange(properties.getDeadExchange())
                .deadLetterRoutingKey(properties.getDeadRoutingKey())
                .maxLength(1000)
                .ttl(5000)
                .build();
    }

    @Bean
    public Binding bulletScreenQueueBindingBulletScreenExchange(
            @Qualifier("bulletScreenQueue") Queue bulletScreenQueue
            ,@Qualifier("bulletScreenExchange")  Exchange bulletScreenExchange) {
        return BindingBuilder.bind(bulletScreenQueue).to(bulletScreenExchange).with(properties.getRoutingKey()).noargs();
    }

    @Bean
    public Exchange deadBulletScreenExchange() {
        return ExchangeBuilder.topicExchange(properties.getDeadExchange())
                .durable(true)
                .build();
    }

    @Bean
    public Queue deadBulletScreenQueue() {
        return QueueBuilder.durable(properties.getDeadQueue()).ttl(10000).build();
    }

    @Bean
    public Binding deadBulletScreenBindingDeadBulletScreenExchange(
            @Qualifier("deadBulletScreenQueue") Queue deadBulletScreenQueue
            , @Qualifier("deadBulletScreenExchange") Exchange deadBulletScreenExchange) {
        return BindingBuilder.bind(deadBulletScreenQueue).to(deadBulletScreenExchange).with(properties.getDeadRoutingKey()).noargs();
    }

    @Bean
    public Exchange backupBulletScreenExchange() {
        return ExchangeBuilder.fanoutExchange(properties.getBackupExchange()).durable(true).build();
    }

    @Bean
    public Queue backupBulletScreenQueue() {
        return QueueBuilder.durable(properties.getBackQueue()).build();
    }

    @Bean
    public Queue warningBulletScreenQueue() {
        return QueueBuilder.durable(properties.getWarningQueue()).build();
    }

    @Bean
    public Binding backBulletScreenQueueBindingBackBulletScreenExchange(
            @Qualifier("backupBulletScreenQueue") Queue backupBulletScreenQueue
            , @Qualifier("backupBulletScreenExchange") Exchange backupBulletScreenExchange) {
        return BindingBuilder.bind(backupBulletScreenQueue).to(backupBulletScreenExchange).with("").noargs();
    }

    @Bean
    public Binding warningBulletScreenQueueBindingBackBulletScreenExchange(
            @Qualifier("backupBulletScreenQueue") Queue backupBulletScreenQueue
            , @Qualifier("backupBulletScreenExchange") Exchange backupBulletScreenExchange) {
        return BindingBuilder.bind(backupBulletScreenQueue).to(backupBulletScreenExchange).with("").noargs();
    }
}